﻿
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace HttpHelper
{

    public class UploadInfo
    {
        public int ErrorCode = -1;
        public string ErrMsg = string.Empty;
    }
    public class UploadPackageInfo
    {
        public string Title = string.Empty;
        public string FilePath = string.Empty;
    }
    
    public class NetCommunication
    {

        private static string GetPlatformURL()
        {
            return "http://112.74.196.127:8080/jszx/scoreQuestions/saveQuestions";
            return "http://112.74.196.127:8080/jszx/scoreQuestions/saveQuestions?sign=YbqUPaWLIr1L+9IzPlDtJtAZl10HM4xF2R9TVfz8yMCWWw4eHkOEMe+lUuPHQio5X9JtnmwZWMNzhmXHmvqY96C6Cr5XSdnaCXgdyv+mSRlgyD+U6g77vU4Co22twNx19KCQ2YWQP0PScuCqigQjFrWN4FYs9LXL1iOzxSGblv0=";

        }
        string SignRSA(string content)
        {
            string privateJavaKey = @"MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAI2yr2nRFVk9t0R99xF3ZB0LCc4tAR6YOwioa0WQILaO2OIbaf6TW1Tyly+fnP5m+Qeynzt5WSGtljuTWNUSy2d9x7hYRhUtHYteE5ohMWvrkAkz/MG0JrjEedUNc0pbc6WtJed2rjlkakzdmtRKd0J1Fbxlap36z2L7yD+3zF0xAgMBAAECgYAVBayGm3F6YvCcpb+RBNCCio+y2fRvkJA5kZsQn/hqfTjqGr0X+s6zBKavdxO4nphVrwd2sGMr3vAJ+KtWOUqZ32LlaQT7g1RVEbjU3EGOayIdvMd7/nZ0cvB+I8gcrkZQa+8szfuhGEX7+yoL59O2brDof2Fz80jMpyoaw8VSvQJBAOjrCuF9YZXF4j48sb/2pVehXDKCm7UY2TfzdeJOdbRu6DL7FOQcx1LLDkqd8G7ByVFCCi2VgYQ9IpS49vZy1TsCQQCbvXTU9CGCBQ4YjIMWrijLXihVxx1wz7ZHzpCOG/INVZsBbn7rjMHFL5n1lybX5rPDxMMxYhHVQhSXbtyLh8CDAkA7YyKlF9j33Tqwb71KmtLRoxzk7BRTeoB4GaVnDGZKmTSvOwG6n/PakleUguy48MQp6dK6iLI7S3MzxG2DrQfdAkAR+iubTVb02DqJUObs3kutNgA37pUSaZej8E81aDpnmaMb5jJWZFgVt+nwtHLyZxoHFhZmA+kFEeyokk54+MGbAkAzkc3rCx5/GOwKnFkaDy/kTwkeDzKoWbgAuPKtDeUpP7pUaABe3fZv1MZAeEvCrfMo1rWdSFyLT1hBpHCB56dg";
            string privateCSharpKey = RSAConverter.RSAPrivateKeyJava2DotNet(privateJavaKey);
            
            RSACryptoServiceProvider provider = new RSACryptoServiceProvider();
            provider.FromXmlString(privateCSharpKey);
            string halg = "MD5";//SHA1 MD5 SHA256  
            string signData = RSAHelper.SignData(privateCSharpKey, content, halg);//SHA1  
            return signData;
        }

        public  UploadInfo PostMicrotestData(UploadPackageInfo videoInfo,string url)
        {
            HttpClient httpClient = new HttpClient();
            httpClient.Url = url;// GetPlatformURL();
            string sign = SignRSA(videoInfo.Title);
            
            //httpClient.PostingData.Add("sign", "YbqUPaWLIr1L+9IzPlDtJtAZl10HM4xF2R9TVfz8yMCWWw4eHkOEMe+lUuPHQio5X9JtnmwZWMNzhmXHmvqY96C6Cr5XSdnaCXgdyv+mSRlgyD+U6g77vU4Co22twNx19KCQ2YWQP0PScuCqigQjFrWN4FYs9LXL1iOzxSGblv0=");
            httpClient.PostingData.Add("sign", sign);
            httpClient.UploadStatus += httpClient_UploadStatus;
            HttpUploadingFile file = new HttpUploadingFile(videoInfo.FilePath, "filePath");
            httpClient.Files.Add(file);
            string msg = string.Empty;
            
            string respondStr = httpClient.GetStringWithMsg(Encoding.UTF8,ref msg);
            if(respondStr==null)
            {
                
                UploadInfo info = new UploadInfo();
                info.ErrorCode = -1;
                info.ErrMsg = msg;
                return info;
            }
            if (respondStr != null && respondStr.Length > 0)
            {
                UploadInfo upinfo = new UploadInfo();
                try
                {
                    JObject jobj = JObject.Parse(respondStr);
                    upinfo.ErrorCode = Convert.ToInt32(jobj["code"]);
                    upinfo.ErrMsg = jobj["message"].ToString();
                }
                catch (Exception)
                {
                    return null;
                }
                return upinfo;
            }
            return null;
        }
        public event  EventHandler<StatusUpdateEventArgs> UploadStatus;
        void httpClient_UploadStatus(object sender, StatusUpdateEventArgs e)
        {
            if (UploadStatus != null)
                UploadStatus(sender, e);
        }

        

        public static string GetMD5(string s)
        {
            string sMD5 = string.Empty;

            MD5CryptoServiceProvider md5CSP = new MD5CryptoServiceProvider();
            //获取要加密的字段，并转化为Byte[]数组  
            byte[] testEncrypt = System.Text.Encoding.UTF8.GetBytes(s);
            //加密Byte[]数组  
            byte[] resultEncrypt = md5CSP.ComputeHash(testEncrypt);
            foreach (byte InByte in resultEncrypt)
            {
                sMD5 = (sMD5 + String.Format("{0:x2} ", InByte)).Trim();
            }
            return sMD5;
        }

        internal static byte[] GetMD5Bytes(string s)
        {
            string sMD5 = string.Empty;

            MD5CryptoServiceProvider md5CSP = new MD5CryptoServiceProvider();
            //获取要加密的字段，并转化为Byte[]数组  
            byte[] testEncrypt = System.Text.Encoding.UTF8.GetBytes(s);
            //加密Byte[]数组  
            byte[] resultEncrypt = md5CSP.ComputeHash(testEncrypt);
            return resultEncrypt;
        }

        /// <summary>
        /// MD5 16位加密 加密后密码为大写
        /// </summary>
        /// <param name="ConvertString"></param>
        /// <returns></returns>
        internal static string GetMd5Str(string ConvertString)
        {
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            string t2 = BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(ConvertString)), 4, 8);
            t2 = t2.Replace("-", "");
            return t2;
        }

       
    }
}
